package com.zzsy.shoppingpro.home.acitivty;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.RadioButton;


import com.zzsy.shoppingpro.R;

import com.zzsy.shoppingpro.base.BaseActivity;
import com.zzsy.shoppingpro.home.fragment.fragmentNavigator.FragmentNavigator;
import com.zzsy.shoppingpro.home.fragment.fragmentNavigator.HomeNavigatorAdapter;

import com.zzsy.shoppingpro.utils.ActivityCollector;
import com.zzsy.shoppingpro.utils.StatusBarUtil;
import com.zzsy.shoppingpro.utils.Toast;



import java.util.Timer;
import java.util.TimerTask;

public class HomeActivity extends BaseActivity implements View.OnClickListener{

    private FragmentNavigator navigator;
    private RadioButton rbHome,rbType,rbCommunity,rbCart,rbUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置无ActionBar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 设置只竖屏显示
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        StatusBarUtil.setStatusBarColor(this, setStatusBarColor());
        setContentView(R.layout.activity_home);
        initUI();
    }
    /**
     * 设置各个页面的状态栏颜色
     *
     * @return 颜色值
     */
    protected int setStatusBarColor() {
        return 0;
    }

    /**
     * 初始化数据
     */
    private void initUI() {

        //展示各个fragment的视图控件
        rbHome = findViewById(R.id.rb_home);
        rbType = findViewById(R.id.rb_type);
        rbCommunity = findViewById(R.id.rb_community);
        rbCart = findViewById(R.id.rb_cart);
        rbUser = findViewById(R.id.rb_user);
        rbHome.setOnClickListener(this);
        rbType.setOnClickListener(this);
        rbCommunity.setOnClickListener(this);
        rbCart.setOnClickListener(this);
        rbUser.setOnClickListener(this);

        navigator = new FragmentNavigator(getSupportFragmentManager(),new HomeNavigatorAdapter(),R.id.fragment_layout);
        rbHome.setChecked(true);
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rb_home:
                navigator.showFragment(0);
                break;

            case R.id.rb_type:
                navigator.showFragment(1);
                break;

          case R.id.rb_community:
                navigator.showFragment(2);
                break;

            case R.id.rb_cart:
                navigator.showFragment(3);
                break;
            case R.id.rb_user:
                navigator.showFragment(4);
                break;
            default:
                navigator.showFragment(0);
                break;
        }
    }


    /**
     * 双击返回按钮逻辑实现
     */

    private static boolean mBackKeyPressed = false; //记录是否有首次按键

    @Override
    public void onBackPressed() {
        if(!mBackKeyPressed){
            Toast.show(HomeActivity.this,"再按一次退出程序！");
            mBackKeyPressed = true;
            new Timer().schedule(new TimerTask() { //延时两秒 如果超出 去除第一次按键记录
                @Override
                public void run() {
                    mBackKeyPressed = false;
                }
            },2000);
        }else{
            ActivityCollector.finishAll();
        }
    }

}
